package com.zxh.listdemo.adapter.list;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zxh.listdemo.Config;
import com.zxh.listdemo.ListActivity;
import com.zxh.listdemo.R;
import com.zxh.listdemo.adapter.list.common.BaseMultiModel;
import com.zxh.listdemo.model.OneModel;
import com.zxh.listdemo.model.TwoModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangxuehui on 2018/3/5.
 */

public class MultiAdapter extends BaseAdapter {

    private List<BaseMultiModel> mList = new ArrayList<>();
    private Context mContext;

    public MultiAdapter(List<BaseMultiModel> mList, Context mContext) {
        this.mList = mList;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public BaseMultiModel getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return mList.get(position).getItemType();
    }

    @Override
    public int getViewTypeCount() {

        return 22;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.e(this.getClass().getSimpleName(), getViewTypeCount() + "==="+ getItemId(position)+"==="+getItem(position).getItemType()+"===" + getItemViewType(position));
        if (getItemViewType(position) == Config.ITEM_TEXT) {
            TextViewHolder mHolder;
            if (convertView == null) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_two, parent, false);
                mHolder = new TextViewHolder(convertView);
                convertView.setTag(mHolder);
            } else {
                mHolder = (TextViewHolder) convertView.getTag();
            }
            mHolder.mTvDesc.setText(((TwoModel) getItem(position)).getContent());
        } else if(getItemViewType(position) == Config.ITEM_IMG){
            ImgViewHolder mHolder = null;
            if (null == convertView) {
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_one, parent, false);
                mHolder = new ImgViewHolder(convertView);
                convertView.setTag(mHolder);
            } else {
                mHolder = (ImgViewHolder) convertView.getTag();
            }
            OneModel model = (OneModel) getItem(position);
            mHolder.mTvTitle.setText(model.getTitle());
            mHolder.mTvDesc.setText(model.getDesc());
            Log.e(ListActivity.class.getSimpleName(), model.getLink());
//            Picasso.with(mContext).load(model.getLink()).resize(100,100).into(mHolder.mIvIcon);
        }

        return convertView;
    }

    /**
     * 文字布局的viewholder
     */
    public static class TextViewHolder {
        TextView mTvDesc;

        TextViewHolder(View view) {
            this.mTvDesc = (TextView) view.findViewById(R.id.tv_desc);
        }
    }

    public static class ImgViewHolder {
        View view;
        ImageView mIvIcon;
        TextView mTvTitle;
        TextView mTvDesc;

        ImgViewHolder(View view) {
            this.view = view;
            this.mIvIcon = (ImageView) view.findViewById(R.id.iv_icon);
            this.mTvTitle = (TextView) view.findViewById(R.id.tv_title);
            this.mTvDesc = (TextView) view.findViewById(R.id.tv_desc);
        }
    }
}
